0// 防抖
// 触发函数delay秒后执行，若在delay内再次触发则重新计时delay秒后执行
function debound (fn, delay) {
    let timer = null;
    return function (...args) {
        if (timer) {
            clearInterval(timer);
            timer = null;
        }
        const self = this
        timer = setInterval(() => {
            fn.apply(self, args)
            timer = null
        }, delay)
    }
}
// 节流 
// 若在单位时间内多次触发函数，则只执行函数一次
function trottle(fn, wait) {
    let last = 0, timer = null;
    return function (...args) {
        if (timer) {
            clearInterval(timer)
            timer = null
        }
        const that = this, now = 1*new Date(),duration = last+wait-now
        if (duration < 0) {
            setInterval(() => {
                last = +new Date()
                fn.apply(that, args)
                timer = null
            }, duration)
        }else{
            last = now
            fn.apply(that, args)
        }
    }
}
//  只要进入函数就要注意 this指向
// 注意函数fn1作为参数传入fn2，fn1中的this指向与fn2的不一致
// 包装函数要注意this指向问题
const baozhuang = function (fn) {
    return function () {
        console.log('out this ', this) // this --- b
        fn()
    }
}
function a1 () {
    console.log('a1 this ', this) // this --- window
}
const t = baozhuang(a1)
const b = {
    a: 12,
    c: 13,
    t
}
b.t()